const {
  get,
  post,
  put,
  controller
} = require('../lib/decorator')
import {
  checkPassword
} from '../service/admin'

@controller('/api/v0/user')
export class userController {
  @post('/')
  async getUsers(ctx, next) {
    if (ctx.session.user) {
      return (ctx.body = {
        success: false,
        message: '请勿重复登录~'
      })
    }
    let { username, pass } = ctx.request.body
    const { user, match } = await checkPassword(username, pass)

    
    if (match) {
      ctx.session.user = {
        _id: user._id,
        email: user.email,
        role: user.role,
        username: user.username
      }
      return (ctx.body = {
        success: true,
        user: user.username,
        email: user.email
      })
    }
    return (ctx.body = {
      success: false,
      message: '用户名或密码错误，检查后重试'
    })
  }
  @post('/logout')
  async logout(ctx, next) {
    ctx.session = null
    return (ctx.body = {
      success: true,
      message: '退出成功'
    })
  }
}
